/*本模块功能:排课时间设置
        编写者:朱光曜
        编写时间:2017.11.20*/
$(function() {
	var Alert1 = new Alert;
	var confirm2 = new Confirm;
	var loading1 = new Loading();//监控全局ajax添加loading
	loading1.init();
    $(document).ajaxSend(function(){
		loading1.show();
		
		});
	$(document).ajaxStop(function(){
		loading1.hide();
	
		});
	var index = {
		init: function() {
			this.bind();
		},
		bind: function() {
			var self = this;
			self.load();
			datePicker.datePickerTimes($(".form_date"));//时间插件
			var setTheTime=function(event){
			    $(".datetimepicker").hide();
			    event.stopPropagation();
			};
            $(".table-condensed .prev").remove();
            $(".table-condensed .next").remove();
            $(".switch").html("课程时间");
            // $(".table-condensed .datetimepicker-minutes span").width("31%");
			$(document).off('mousedown touchend').on('mousedown touchend','.mainContent', setTheTime);
			$(".preservation").off("click").click(function() {//点击保存
				var $_self = self;

				function getFileName(o) { //处理字符串
					var pos = o.lastIndexOf(":");
					return o.substring(0, pos - 1);
				}

				function getTime(o) { //处理字符串
					var pos = o.lastIndexOf(":");
					return o.substring(0, pos) + o.substring(pos + 1);
				}
				
				if($(".mainContent .timeContent .timeContentSelect .am1 input.form-control").val() == "") {
					var str = "请选择第一节时间";
					self.alert1(str);
				} else if(getTime($("#am1_1").val()) > getTime($("#am1_2").val())) {
					var str = "开始时间应该在结束时间之前";
					self.alert1(str);
				} else if($(".mainContent .timeContent .timeContentSelect .am2 input.form-control").val() == "") {
					var str = "请选择第二节时间";
					self.alert1(str);
				} else if(getTime($("#am2_1").val()) > getTime($("#am2_2").val())) {
					var str = "开始时间应该在结束时间之前";
					self.alert1(str);
				} else if($(".mainContent .timeContent .timeContentSelect .pm1 input.form-control").val() == "") {
					var str = "请选择第三节时间";
					self.alert1(str);
				} else if(getTime($("#pm1_1").val()) > getTime($("#pm1_2").val())) {
					var str = "开始时间应该在结束时间之前";
					self.alert1(str);
				} else if($(".mainContent .timeContent .timeContentSelect .pm2 input.form-control").val() == "") {
					var str = "请选择第四节时间";
					self.alert1(str);
				} else if(getTime($("#pm2_1").val()) > getTime($("#pm2_2").val())) {
					var str = "开始时间应该在结束时间之前";
					self.alert1(str);
				} else if($(".mainContent .timeContent .timeContentSelect .noon1 input.form-control").val() == "") {
					var str = "请选择第五节时间";
					self.alert1(str);
				} else if(getTime($("#noon1_1").val()) > getTime($("#noon1_2").val())) {
					var str = "开始时间应该在结束时间之前";
					self.alert1(str);
				} else {
					var parameter = {
						"upOneStart": $("#am1_1").val(),
						"upOneEnd": $("#am1_2").val(),
						"upTwoStart": $("#am2_1").val(),
						"upTwoEnd": $("#am2_2").val(),
						"downOneStart": $("#pm1_1").val(),
						"downOneEnd": $("#pm1_2").val(),
						"downTwoStart": $("#pm2_1").val(),
						"downTwoEnd": $("#pm2_2").val(),
						"bottomOneStart": $("#noon1_1").val(),
						"bottomOneEnd": $("#noon1_2").val(),

					};  
					var keys = [];
                    for (key in parameter) {
					    keys.push(parameter[key]);
					}
                   
                    for(var j=0;j<keys.length;j++){
						
						if(keys[j+1]){
						if(getTime(keys[j])>getTime(keys[j+1])){
							
							var str = "你输入的时间有误请重新输入";
						
							self.alert1(str);
							return;
						}
						}
					}
                    var parameter1 = {
    						"upOneStart": $("#am1_1").val(),
    						"upOneEnd": $("#am1_2").val(),
    						"upTwoStart": $("#am2_1").val(),
    						"upTwoEnd": $("#am2_2").val(),
    						"downOneStart": $("#pm1_1").val(),
    						"downOneEnd": $("#pm1_2").val(),
    						"downTwoStart": $("#pm2_1").val(),
    						"downTwoEnd": $("#pm2_2").val(),
    						"bottomOneStart": $("#noon1_1").val(),
    						"bottomOneEnd": $("#noon1_2").val(),
                            "changeMark":1
    					};
                    var parameter2 = {
    						"upOneStart": $("#am1_1").val(),
    						"upOneEnd": $("#am1_2").val(),
    						"upTwoStart": $("#am2_1").val(),
    						"upTwoEnd": $("#am2_2").val(),
    						"downOneStart": $("#pm1_1").val(),
    						"downOneEnd": $("#pm1_2").val(),
    						"downTwoStart": $("#pm2_1").val(),
    						"downTwoEnd": $("#pm2_2").val(),
    						"bottomOneStart": $("#noon1_1").val(),
    						"bottomOneEnd": $("#noon1_2").val(),
    						"changeMark":0
    					};
                    self.confirm2("是否将此设置应用至已有课表",parameter1,parameter2);
					
				}

			});
		},
		load: function() { //加载页面基本信息
			var parameter = {};
			$.ajax({
				type: "post",
				url: "skipParamMana",
				async: true,
				data: {
					"jsonStr": JSON.stringify(parameter)
				},

				success: function(msg) {

					var msgNew = JSON.parse(msg);
					$("#am1_1").val(msgNew.upOneStart);
					$("#am1_2").val(msgNew.upOneEnd);
					$("#am2_1").val(msgNew.upTwoStart);
					$("#am2_2").val(msgNew.upTwoEnd);
					$("#pm1_1").val(msgNew.downOneStart);
					$("#pm1_2").val(msgNew.downOneEnd);
					$("#pm2_1").val(msgNew.downTwoStart);
					$("#pm2_2").val(msgNew.downTwoEnd);
					$("#noon1_1").val(msgNew.bottomOneStart);
					$("#noon1_2").val(msgNew.bottomOneEnd);

				},
				error: function(data, status, xhr) {
					var json = JSON.stringify({
						"pageName": "error"
				    });
					window.location.href = "skipPage?jsonStr=" + json;

				}
			});
		},
		delivery: function(parameter) {//保存事件
			var $self = this;
            
			$.ajax({
				type: "post",
				url: "saveParam",
				async: true,
				data: {
					"jsonStr": JSON.stringify(parameter)
				},

				success: function(msg) {

					var msgNew = JSON.parse(msg);
					var self = $self;
					if(msgNew.result == "success") {
						self.alert1("保存成功","success");

					} else {
						//self.alert1(msgNew.failMsg);
						if(msgNew.failMsg){
							self.alert1(msgNew.failMsg);
						}else{
							self.alert1("保存失败");
						}

					}

				},
				error: function(data, status, xhr) {
				var json = JSON.stringify({
					"pageName": "error"
			    });
				window.location.href = "skipPage?jsonStr=" + json;

				}
			});
		},
		alert1: function(content1,state) { //confirm提示

			Alert1.contenthead = "提示";
			Alert1.content = content1;

			Alert1.success = function() {
				if(state=="success"){
					window.location.reload();
				}
				
				Alert1.hide();
			};

			Alert1.init();
			Alert1.show();
		},
		confirm2: function(content1,parameter1,parameter2) { //confirm提示
            var self=this;
			confirm2.contenthead = "提示";
			confirm2.content = content1;

			confirm2.success =function(){
				self.delivery(parameter1);
			};
			confirm2.fail = function(){
			
				self.delivery(parameter2);
			};
			
			confirm2.init();
			$(".alert_img").hover(function(){
				confirm2.fail = function(){
					
					confirm2.hide();
				};
			},function(){
				confirm2.fail = function(){
					
					self.delivery(parameter2);
				};
			});
			confirm2.show();
		}

	};
	index.init();
});